//
//  ABCDynamicManager.m
//  ABCAppearanceKit_Example
//
//  Created by mac on 2022/9/1.
//  Copyright © 2022 Joe. All rights reserved.
//

#import "ABCDynamicManager.h"
#import "ABCDynamicUpdater.h"
#import "ABCDynamicConst.h"
#import "Aspects.h"

static BOOL _isUpdateFollowSystem = NO;
static ABCAppearanceDynamicStyle _currentStyle = ABCAppearanceDynamicStyle_Light;

@implementation ABCDynamicManager

+ (void)load {
    BOOL isUpdateFollowSystem = [[NSUserDefaults standardUserDefaults] boolForKey:kCacheDynamic_isUpdateFollowSystem];
    [self setIsUpdateFollowSystem:isUpdateFollowSystem];
    
    NSInteger currentDynamicStyle =  [[NSUserDefaults standardUserDefaults] integerForKey:kCacheDynamic_currentDynamicStyle];
    if (currentDynamicStyle != 0) {
        [self setCurrentDynamicStyle:currentDynamicStyle];
    }
    
    // hook UIWindow
    [UIWindow aspect_hookSelector:@selector(traitCollectionDidChange:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info, UITraitCollection *previousTraitCollection) {
        if (@available(iOS 13.0, *)) {
            if (previousTraitCollection.userInterfaceStyle != [UITraitCollection currentTraitCollection].userInterfaceStyle) {
                if ([self isUpdateFollowSystem]) {
                    [self setCurrentDynamicStyle:([UITraitCollection currentTraitCollection].userInterfaceStyle == UIUserInterfaceStyleLight ? ABCAppearanceDynamicStyle_Light : ABCAppearanceDynamicStyle_Dark)];
                }
                [[ABCDynamicUpdater sharedInstance] abc_updateDynamicStyle:self.currentDynamicStyle];
            }
        }
    } error:NULL];
}

+ (BOOL)isSupportDynamic {
    if (@available(iOS 13.0, *)) {
        return YES;
    }
    return NO;
}

+ (void)setIsUpdateFollowSystem:(BOOL)isUpdateFollowSystem {
    _isUpdateFollowSystem = isUpdateFollowSystem;
    [[NSUserDefaults standardUserDefaults] setBool:isUpdateFollowSystem forKey:kCacheDynamic_isUpdateFollowSystem];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self updateAllDynamic];
}

+ (BOOL)isUpdateFollowSystem {
    return _isUpdateFollowSystem;
}

+ (ABCAppearanceDynamicStyle)currentDynamicStyle {
    if ([self isUpdateFollowSystem]) {
        _currentStyle = ABCAppearanceDynamicStyle_Light;
        if (@available(iOS 13.0, *)) {
            if([UITraitCollection currentTraitCollection].userInterfaceStyle == UIUserInterfaceStyleDark){
                _currentStyle = ABCAppearanceDynamicStyle_Dark;
            }
        }
    }
    return _currentStyle;
}

+ (void)setCurrentDynamicStyle:(ABCAppearanceDynamicStyle)currentDynamicStyle {
    _currentStyle = currentDynamicStyle;
    [[NSUserDefaults standardUserDefaults] setInteger:currentDynamicStyle forKey:kCacheDynamic_currentDynamicStyle];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self updateAllDynamic];
}

+ (void)updateAllDynamic {
    if (@available(iOS 13.0, *)) {
        for (UIWindow *window in [UIApplication sharedApplication].windows) {
            window.overrideUserInterfaceStyle = self.isUpdateFollowSystem ? UIUserInterfaceStyleUnspecified : (self.currentDynamicStyle);
        }
    }
}

+ (void)p_windowBecomeVisible:(NSNotification *)notification {
    if ([notification.object isKindOfClass:[UIWindow class]]) {
        UIWindow *window = notification.object;
        if (@available(iOS 13.0, *)) {
            window.overrideUserInterfaceStyle = self.isUpdateFollowSystem ? UIUserInterfaceStyleUnspecified : (self.currentDynamicStyle);
        }
    }
}

@end
